import json

import networkx as nx
import numpy as np
import pandas as pd


adj_matrix=[
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
        [0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
        [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
]
with open('profiles.json', 'r') as f:
    agents = json.load(f)
# 初始化三维矩阵
big5_matrix = [[0]*5 for _ in range(30)]
for i in range(30):
    agent = agents[i]
    big5 = agent['big5']
    big5_matrix[i] = [
        big5['O'],
        big5['C'],
        big5['E'],
        big5['A'],
        big5['N']
    ]

belief_values=[
    [0.0, 0.6, 1.0, 1.4, 1.7, 1.9, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.8, 1.7, 1.8, 1.8, 2.0, 2.0, 2.0, 2.0, 2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, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 1.8, 1.9],
    [0.8, 1.3, 1.6, 1.9, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.9, 1.9],
    [1.0, 1.5, 1.8, 2.0, 2.0, 2.0, 2.0, 1.8, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.9, 1.9]
]
# 假设已有以下数据：
np_adj_matrix = np.array(adj_matrix)          # 30x30邻接矩阵
np_big5_matrix = np.array(big5_matrix)        # 30x30x5三维矩阵（实际应为30x5）
np_belief_values = np.array(belief_values)    # 30x10信念值矩阵

# 计算网络特征
G = nx.from_numpy_array(np_adj_matrix)
degrees = np.array(list(dict(G.degree()).values()))                   # 节点度数
betweenness = np.array(list(nx.betweenness_centrality(G).values()))   # 介数中心性

# 创建DataFrame
df = pd.DataFrame({
    "agent_id": np.repeat(np.arange(30), 30),     # 每个Agent重复10次（10轮）
    "round": np.tile(np.arange(30), 30),          # 轮次编号0-9
    "belief": np_belief_values.flatten(),           # 展平信念值矩阵
    "degree": np.repeat(degrees, 30),
    "betweenness": np.repeat(betweenness, 30),
    "O": np.repeat(np_big5_matrix[:, 0], 30),       # 开放性
    "C": np.repeat(np_big5_matrix[:, 1], 30),       # 尽责性
    "E": np.repeat(np_big5_matrix[:, 2], 30),       # 外向性
    "A": np.repeat(np_big5_matrix[:, 3], 30),       # 宜人性
    "N": np.repeat(np_big5_matrix[:, 4], 30)        # 神经质
})

# 添加信念值变化量（需按Agent分组计算）
df['belief_change'] = df.groupby('agent_id')['belief'].diff().fillna(0)

import seaborn as sns
import matplotlib.pyplot as plt

# 绘制所有Agent的信念值变化曲线
plt.figure(figsize=(12, 6))
sns.lineplot(data=df, x='round', y='belief', hue='agent_id', palette='viridis', alpha=0.3)
plt.title("Belief Evolution Across Rounds")
plt.xlabel("Round")
plt.ylabel("Belief Value")
plt.legend().remove()
plt.show()

# 计算节点中心性与平均信念值的相关性
corr_degree = np.corrcoef(degrees, df.groupby('agent_id')['belief'].mean())[0, 1]
corr_betweenness = np.corrcoef(betweenness, df.groupby('agent_id')['belief'].mean())[0, 1]
print(f"度数与信念值相关性: {corr_degree:.2f}")
print(f"介数与信念值相关性: {corr_betweenness:.2f}")

# 计算Big5各维度与信念值的平均相关性
corr = df[['O', 'C', 'E', 'A', 'N', 'belief']].corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title("Personality-Belief Correlation")
plt.show()
